package cn.edu.hizee.gps.dao;


import cn.edu.hizee.gps.model.Contact;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;
import java.util.Map;

/**
 * @author ZX
 */
public interface ContactRepository extends JpaRepository<Contact,Integer>, CrudRepository<Contact,Integer> {

    /**
      *@Author
      *@Date: 2018/11/19 10:35
      *@return
    **/
    @Query(nativeQuery = true, value = "SELECT " +
            "t_contact.id, " +
            "t_contact.user_id, " +
            "t_contact.contact_name, " +
            "t_contact.contact_phone, " +
            "t_contact.is_disable, " +
            "t_contact.STATUS, " +
            "t_contact.contact_password, " +
            "t_user_info.sex " +
            "FROM " +
            "t_contact " +
            "JOIN t_user_contact ON t_user_contact.user_id = :userId " +
            "AND t_user_contact.contact_id = t_contact.id " +
            "JOIN t_user_info on t_user_info.user_id = t_contact.user_id ")
    List<Map> findContactList(@Param("userId") int userId);

    Contact findAllByContactPhoneAndContactPassword(String phone, String password);

    Contact findByContactPhoneAndContactPassword(String phone, String password);

    /**
     * 查找联系人id
     * @param userId
     * @return
     */
    @Query(nativeQuery = true, value = "SELECT " +
            "t_contact.user_id " +
            "FROM " +
            "t_contact  " +
            "JOIN t_user_contact on t_user_contact.user_id = :userId AND t_contact.user_id = t_user_contact.contact_id ")
    List<Integer> findALLContactId(@Param("userId") String userId);

    /**
     * 查找联系人
     * @param userId
     * @return
     */
    @Query(nativeQuery = true, value = "SELECT " +
            "t_contact.id, " +
            "t_contact.user_id, " +
            "t_contact.contact_name, " +
            "t_contact.contact_phone, " +
            "t_contact.is_disable, " +
            "t_contact.STATUS, " +
            "t_contact.contact_password " +
            "FROM " +
            "t_contact " +
            "JOIN t_user_contact ON t_user_contact.user_id = :userId " +
            "AND t_user_contact.contact_id = t_contact.id")
    List<Contact> findContact(@Param("userId") Integer userId);

    @Query(nativeQuery = true, value = "SELECT " +
            "t_contact.id, " +
            "t_contact.user_id, " +
            "t_contact.contact_name, " +
            "t_contact.contact_phone, " +
            "t_contact.contact_password, " +
            "t_contact.is_disable, " +
            "t_contact.status " +
            "FROM " +
            "t_contact " +
            "JOIN t_user_contact ON t_user_contact.contact_id = t_contact.id AND t_user_contact.user_id = :userId ")
    List<Contact> findAllContact(@Param("userId") int userId);


    @Query(nativeQuery = true, value = "SELECT " +
            "*  " +
            "FROM " +
            "t_contact " +
            "LIMIT :pageNum, :pageAmount")
    List<Contact> findPages(int pageNum, int pageAmount);

}
